﻿using NDK.Module.Model;
using NDK.Module.StationLogic;
using NDK.Motion.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Module.BLL
{
    public delegate void delegateUpdateUI(MeasureProduct product);
    public delegate void delegateUpdateNGUI(MeasureProduct product);

    class ProcessData
    {
        //public static int i = 0;
        public static event delegateUpdateUI UpdateUIEvent;
        public static event delegateUpdateNGUI UpdateNGUIEvent;

        public static int SortMeasureSize(DataItem x, DataItem y)
        {
            if (x.ID > y.ID)
                return 1;
            if (x.ID < y.ID)
                return -1;
            return 0;
        }

        /// <summary>
        /// 结果判断进行分料
        /// </summary>
        /// <param name="measureProduct"></param>
        /// <returns></returns>
        public static JUDGEMENT_RESULT PrcessData(MeasureProduct measureProduct)
        {
            JUDGEMENT_RESULT ret = Jugement.JugeMeasureData(ref measureProduct);

            measureProduct.MeasureData.Sort(SortMeasureSize);//按照ID排序

            #region 刷新数据界面  保存数据csv  苹果上传数据
            if (ret == JUDGEMENT_RESULT.OK)
            {
                measureProduct.OK_NG = "OK";
                Common.TotalCount++;
                if (Common.CurProType == "Product")
                {
                    Common.OEEYieldMethod.Refresh(DateTime.Now, 1, 1);
                }
                UpdateUIEvent.BeginInvoke(measureProduct, null, null);
            }
            else if (ret == JUDGEMENT_RESULT.NG)
            {
                measureProduct.OK_NG = "NG";
                Common.NGCount++;
                Common.TotalCount++;
                if (Common.CurProType == "Product")
                {
                    var whoNG = measureProduct.NG_FAI.Split('\\').ToList();
                    whoNG.Remove("");
                    if(whoNG!= null)
                    Common.OEEYieldMethod.Refresh(DateTime.Now, 1, 0, whoNG);
                }
                UpdateNGUIEvent.BeginInvoke(measureProduct, null, null);
            }

            #region 良率上传

            bool upload10 = false;
            bool upload12 = false;
            bool upload14 = false;
            bool upload16 = false;
            bool upload18 = false;
            bool upload20 = false;

            bool upload22 = false;
            bool upload0 = false;
            bool upload2 = false;
            bool upload4 = false;
            bool upload6 = false;
            bool upload8 = false;
            var time = DateTime.Now;
            var shift = time.Hour;

            if (shift / 2 == 0 && !upload0 && Common.BP_UPMES)
            {
                upload0 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 1 && upload2 && Common.BP_UPMES)
            {
                upload2 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 2 && upload4 && Common.BP_UPMES)
            {
                upload4 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 3 && upload6 && Common.BP_UPMES)
            {
                upload6 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 4 && upload8 && Common.BP_UPMES)
            {
                upload8 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 5 && upload10 && Common.BP_UPMES)
            {
                upload10 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 6 && upload12 && Common.BP_UPMES)
            {
                upload2 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 7 && upload14 && Common.BP_UPMES)
            {
                upload14 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 8 && upload16 && Common.BP_UPMES)
            {
                upload16 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }


            if (shift / 2 == 9 && upload18 && Common.BP_UPMES)
            {
                upload2 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }


            if (shift / 2 == 10 && upload20 && Common.BP_UPMES)
            {
                upload20 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (shift / 2 == 11 && upload22 && Common.BP_UPMES)
            {
                upload2 = true;
                Common.OEEYieldUpLoadMethod.Upload();
            }

            if (upload10)
            {
                upload22 = false;
                upload0 = false;
                upload2 = false;
                upload4 = false;
                upload6 = false;
                upload8 = false;
            }
            else if (upload22)
            {
                upload10 = false;
                upload12 = false;
                upload14 = false;
                upload16 = false;
                upload18 = false;
                upload20 = false;
            }
            #endregion

            Common.Queue_SaveCsv.Enqueue(measureProduct);
            #endregion

            //产品强制放某个盘   测试流程用
            if (Common.isForceOK)
            {
                ret = JUDGEMENT_RESULT.OK;
            }
            if (Common.isForceNG)
            {
                ret = JUDGEMENT_RESULT.NG;
            }
            if (Common.isForceORT)
            {
                ret = JUDGEMENT_RESULT.ORT;
            }
            return ret;
        }
    }
}


